package com.whenjun.service.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whenjun.common.LoggerParent;
import com.whenjun.entity.IscDoor;
import com.whenjun.entity.IscPerson;
import com.whenjun.service.impl.IscDoorServiceImpl;
import com.whenjun.util.ArtemisPostUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Auther: shirley
 * @Date: 2022/2/13 15:46
 * @Description:
 */
@Service
public class DoorService extends LoggerParent {

    @Autowired
    private ArtemisPostUtil artemisPostUtil;

    @Autowired
    private IscDoorServiceImpl iscDoorService;

    /**
     * @Author shirley
     * @Description  门禁点反控
     * https://open.hikvision.com/docs/affd318425c74441aa3e96e7db3eb450?curNodeId=affd318425c74441aa3e96e7db3eb450#a3b66788
     * @Date 15:48 2022/2/13
     * @Param [requestBody]
     * @return com.alibaba.fastjson.JSONObject
     **/
    public JSONArray doControl(JSONObject requestBody){
        JSONArray responseBody = (JSONArray) artemisPostUtil.callPostApi("/api/acs/v1/door/doControl",requestBody);
        log.info("doControl___req: " + requestBody.toJSONString()
        + "\n" + "doControl___resp: " + responseBody.toJSONString());
        return responseBody;
    }

    // /api/resource/v2/door/search  根据条件查询目录下有权限的门禁点列表。 同步数据至 isc_door
    @Transactional
    public Integer syncDoor(){
        Integer synNumber = 0;

        JSONObject requestBody = new JSONObject();
        requestBody.put("pageNo", 1);
        requestBody.put("pageSize", 1);

        JSONObject jsonObject = (JSONObject) artemisPostUtil.callPostApi("/api/resource/v2/door/search", requestBody);

        // 需要同步的总数
        Integer total = jsonObject.getInteger("total");

        LocalDateTime now = LocalDateTime.now();

        if (total == null) {
            return synNumber;
        }
        //删除数据
        //personMapper.deleteAll(); // 2019.10.21 注释，手动添加不存在的人员
        // 分页量
        Integer pageSize = 1000;
        // 页数计算
        Integer pageCount = (total / pageSize) + 1;

        for (int i = 1; i <= pageCount; i++) {
            requestBody.put("pageNo", i);
            requestBody.put("pageSize", pageSize);

            JSONObject json = (JSONObject) artemisPostUtil.callPostApi("/api/resource/v2/door/search", requestBody);
            JSONArray jsonArray = json.getJSONArray("list");
            List<IscDoor> doorList = JSONObject.parseArray(jsonArray.toJSONString(),
                    IscDoor.class);

            int j = 1;
            for (IscDoor iscDoor : doorList) {
                // 保存
                try {
                    iscDoor.setFalshTime(now);

                    iscDoorService.getBaseMapper().syncDoor(iscDoor);
                } catch (Exception e) {
                    log.debug(e.getMessage());
                    total = total - 1;
                }
                log.debug("第" + i + "组,第" + j + "个");
                j++;
            }
        }
        log.debug("同步门禁的总数： " + jsonObject.getInteger("total"));
        log.debug("同步门禁的实际总数： " + total);
        return total;
    }
}
